package com.xiaoyu.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.xiaoyu.lang.NumEx;

@SuppressWarnings("rawtypes")
public class Convert {

	public static final boolean toBoolean(Object v) {
		if (v == null)
			return false;
		if (v instanceof Boolean) {
			return ((Boolean) v).booleanValue();
		} else if (v instanceof Integer) {
			return ((Integer) v) <= 0 ? false : true;
		} else if (v instanceof Long) {
			return ((Long) v) <= 0 ? false : true;
		} else if (v instanceof String) {
			return NumEx.stringToBool((String) v);
		}
		return false;
	}

	public static final byte toByte(Object v) {
		if (v == null)
			return 0;
		if (v instanceof Byte) {
			return ((Byte) v).byteValue();
		} else if (v instanceof String) {
			return NumEx.stringToByte((String) v);
		}
		return 0;
	}

	public static final short toShort(Object v) {
		if (v == null)
			return 0;
		if (v instanceof Byte) {
			return ((Byte) v).shortValue();
		} else if (v instanceof Short) {
			return ((Short) v).shortValue();
		} else if (v instanceof Integer) {
			return ((Integer) v).shortValue();
		} else if (v instanceof Long) {
			return ((Long) v).shortValue();
		} else if (v instanceof Float) {
			return ((Float) v).shortValue();
		} else if (v instanceof Double) {
			return ((Double) v).shortValue();
		} else if (v instanceof String) {
			return NumEx.stringToShort((String) v);
		} else if (v instanceof BigInteger) {
			return ((BigInteger) v).shortValue();
		} else if (v instanceof BigDecimal) {
			return ((BigDecimal) v).shortValue();
		}
		return 0;
	}

	public static final int toInt(Object v) {
		if (v == null)
			return 0;
		if (v instanceof Byte) {
			return ((Byte) v).intValue();
		} else if (v instanceof Short) {
			return ((Short) v).intValue();
		} else if (v instanceof Integer) {
			return ((Integer) v).intValue();
		} else if (v instanceof Long) {
			return ((Long) v).intValue();
		} else if (v instanceof Float) {
			return ((Float) v).intValue();
		} else if (v instanceof Double) {
			return ((Double) v).intValue();
		} else if (v instanceof String) {
			return NumEx.stringToInt((String) v);
		} else if (v instanceof BigInteger) {
			return ((BigInteger) v).intValue();
		} else if (v instanceof BigDecimal) {
			return ((BigDecimal) v).intValue();
		}
		return 0;
	}

	public static final long toLong(Object v) {
		if (v == null)
			return 0;
		if (v instanceof Byte) {
			return ((Byte) v).longValue();
		} else if (v instanceof Short) {
			return ((Short) v).longValue();
		} else if (v instanceof Integer) {
			return ((Integer) v).longValue();
		} else if (v instanceof Long) {
			return ((Long) v).longValue();
		} else if (v instanceof Float) {
			return ((Float) v).longValue();
		} else if (v instanceof Double) {
			return ((Double) v).longValue();
		} else if (v instanceof String) {
			return NumEx.stringToLong((String) v);
		} else if (v instanceof BigInteger) {
			return ((BigInteger) v).longValue();
		} else if (v instanceof BigDecimal) {
			return ((BigDecimal) v).longValue();
		}
		return 0;
	}

	public static final float toFloat(Object v) {
		if (v == null)
			return (float) 0.0;
		if (v instanceof Byte) {
			return ((Byte) v).floatValue();
		} else if (v instanceof Short) {
			return ((Short) v).floatValue();
		} else if (v instanceof Integer) {
			return ((Integer) v).floatValue();
		} else if (v instanceof Long) {
			return ((Long) v).floatValue();
		} else if (v instanceof Float) {
			return ((Float) v).floatValue();
		} else if (v instanceof Double) {
			return ((Double) v).floatValue();
		} else if (v instanceof String) {
			return NumEx.stringToFloat((String) v);
		} else if (v instanceof BigInteger) {
			return ((BigInteger) v).floatValue();
		} else if (v instanceof BigDecimal) {
			return ((BigDecimal) v).floatValue();
		}
		return 0;
	}

	public static final double toDouble(Object v) {
		if (v == null)
			return 0.0;
		if (v instanceof Byte) {
			return ((Byte) v).doubleValue();
		} else if (v instanceof Short) {
			return ((Short) v).floatValue();
		} else if (v instanceof Integer) {
			return ((Integer) v).doubleValue();
		} else if (v instanceof Long) {
			return ((Long) v).doubleValue();
		} else if (v instanceof Float) {
			return ((Float) v).doubleValue();
		} else if (v instanceof Double) {
			return ((Double) v).doubleValue();
		} else if (v instanceof String) {
			return NumEx.stringToDouble((String) v);
		} else if (v instanceof BigInteger) {
			return ((BigInteger) v).doubleValue();
		} else if (v instanceof BigDecimal) {
			return ((BigDecimal) v).doubleValue();
		}
		return 0;
	}

	public static final String toString(Object v) {
		if (v == null)
			return "";
		else if (v instanceof String)
			return (String) v;

		return String.valueOf(v);
	}

	public static final Date toDate(Object v) {
		if (v == null)
			return null;
		if (v instanceof Date)
			return (Date) v;
		else if (v instanceof NewDate)
			return (NewDate) v;
		else if (v instanceof java.sql.Date)
			return new Date(((java.sql.Date) v).getTime());
		else if (v instanceof java.sql.Timestamp)
			return new Date(((java.sql.Timestamp) v).getTime());
		else if (v instanceof Integer)
			return new Date((Integer) v);
		else if (v instanceof Long)
			return new Date((Long) v);
		else if (v instanceof String)
			return NewDate.parse2((String) v, DateEx.fmt_yyyy_MM_dd_HH_mm_ss);

		return null;
	}

	public static final NewDate toNewDate(Object v) {
		if (v == null)
			return null;
		if (v instanceof NewDate)
			return (NewDate) v;
		else if (v instanceof Date)
			return new NewDate((Date) v);
		else if (v instanceof java.sql.Date)
			return new NewDate(((java.sql.Date) v));
		else if (v instanceof java.sql.Timestamp)
			return new NewDate(((java.sql.Timestamp) v));
		else if (v instanceof Long)
			return new NewDate((Long) v);
		else if (v instanceof String)
			return NewDate.parse2((String) v, DateEx.fmt_yyyy_MM_dd_HH_mm_ss);

		return null;
	}

	public static final Map toMap(Object v) {
		if (v == null)
			return new HashMap();

		if (v instanceof NewMap)
			return (NewMap) v;
		else if (v instanceof Map)
			return (Map) v;

		return new HashMap();
	}

	public static final NewMap toNewMap(Object v) {
		if (v == null)
			return new NewMap();

		if (v instanceof NewMap)
			return (NewMap) v;
		else if (v instanceof Map)
			return NewMap.create((Map) v);

		return new NewMap();
	}

	public static final Set toSet(Object v) {
		if (v == null)
			return new HashSet();

		if (v instanceof NewSet)
			return (NewSet) v;
		else if (v instanceof Set)
			return (Set) v;

		return new HashSet();
	}

	public static final NewSet toNewSet(Object v) {
		if (v == null)
			return new NewSet();

		if (v instanceof NewSet)
			return (NewSet) v;
		else if (v instanceof NewSet)
			return NewSet.create((Set) v);

		return new NewSet();
	}

	public static final List toList(Object v) {
		if (v == null)
			return new Vector();

		if (v instanceof NewList)
			return (NewList) v;
		else if (v instanceof List)
			return (List) v;

		return new Vector();
	}

	public static final NewList toNewList(Object v) {
		if (v == null)
			return new NewList();

		if (v instanceof NewList)
			return (NewList) v;
		else if (v instanceof List)
			return NewList.create((List) v);

		return new NewList();
	}

	public static byte[] toBytes(Object v) {
		if (v == null)
			return new byte[0];
		if (v instanceof byte[])
			return (byte[]) v;
		else if (v instanceof String)
			return ((String) v).getBytes();
		return new byte[0];
	}
}
